package com.stlm2.controller;

import com.stlm2.dems.entity.GoodSpecification;
import com.stlm2.dems.entity.SpecificationContent;
import com.stlm2.dems.service.GoodSpecificationService;
import com.stlm2.dems.service.SpecificationContentService;
import com.stlm2.util.codecontent.CodeContent;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author：fuchunyu
 * Date：2017/9/8
 * Description：
 */
@Controller
@RequestMapping("/specificationContentC")
public class SpecificationContentController {

    @Resource
    private SpecificationContentService specificationContentService;
    @Resource
    private GoodSpecificationService goodSpecificationService;

    @ResponseBody
    @RequestMapping(value = "/getSpecificationContent.json",produces="application/json;charset=UTF-8")
    public String getSpecificationContent(String companyCode,Integer goodid,Integer specificationid){

        List<SpecificationContent> specificationContents=specificationContentService.getSpecificationContent(goodid,specificationid);

        if(specificationContents.size()>0){
            return CodeContent.returnSuccess(specificationContentService.convent(specificationContents));
        }else {
            return CodeContent.returnFailure("获取规格详情失败！");
        }


    }

    @ResponseBody
    @RequestMapping(value = "/getSpecificationContentid.json",produces = "application/json;charset=UTF-8")
    public String getSpecificationContent(String companyCode,Integer goodid){
        Map<Integer, List<SpecificationContent>> map = new HashMap<Integer, List<SpecificationContent>>();

        map =	specificationContentService.getAllValidSpecification(goodid);

        List<SpecificationContent> list= new ArrayList<SpecificationContent>();
        List<List<String>>  listAll = new ArrayList<List<String>>();
        for (Object o : map.keySet()) {
            list = map.get(o);
            List<String>  dd = new ArrayList<String>();
            for(SpecificationContent goodsSpecification:list){
                //String a=o.toString()+":"+goodsSpecification.getContent();
                String a=o.toString()+":"+goodsSpecification.getId().toString();

                dd.add(a);
            }
            listAll.add(dd);
            //list = map.get(o);
        }
        int counterIndex = listAll.size()-1;
        int total  = 1;
        for(List<String> list0: listAll){
            total = list0.size()*total;
        }
        int[] counter =new int[listAll.size()];
        for(int i=0;i<counter.length;i++){
            counter[i]=0;
        }

        List<String> list3 = new ArrayList<String>();
        for(int k=0;k<total;k++){
            List<String> list1 = new ArrayList<String>();
            for(int q = 0;q<listAll.size();q++){
                list1.add(listAll.get(q).get(counter[q]));

            }
            String list1String =  list1.toString();
            list1String=StringUtils.trimAllWhitespace(list1String);
            list1String = list1String.replace("[","{");
            list1String = list1String.replace("]","}");

            list3.add(list1String);

            if(counterIndex>=0)
                handle(counter,counterIndex,listAll);
        }

        //JSONObject json = new JSONObject(list3.toString());
        for (int i=list3.size()-1;i>=0;i--){
            Map<String,Object> map1=new HashMap<String,Object>();
            map1.put("propertiesid",list3.get(i).replace("{","").replace("}","").trim());
            GoodSpecification goodSpecification=goodSpecificationService.getGoodSpecification(map1);
            if(goodSpecification==null)
                list3.remove(i);
        }
        return  CodeContent.returnSuccess(list3);
    }

    public static void handle(int[] counter,int counterIndex,List<List<String>> listAll) {
        counter[counterIndex]++;
        if (counter[counterIndex] >= listAll.get(counterIndex).size()) {
            counter[counterIndex] = 0;
            counterIndex--;
            if (counterIndex >= 0) {
                handle(counter,counterIndex,listAll);
            }
            counterIndex = listAll.size() - 1;
        }
    }
}
